﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class dzsw_anli : System.Web.UI.Page
{
    News n = new News();
    Class c = new Class();

    int sumPage;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["cid"] == null)
        {
            Bind(0);
        }
        else
        {
            Bind(int.Parse(Request["cid"].ToString()));
        }
    }

    private void Bind(int classid)
    {
        int pageNo = 1;
        int pageSize = 12;
        if (Request.QueryString["page"] == null)
        {
            pageNo = 1;
        }
        else
        {
            pageNo = Int32.Parse(Request.QueryString["page"]);
        }


        SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING);
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = new SqlCommand();
        da.SelectCommand.Connection = conn;
        da.SelectCommand.CommandText = "getdataset";
        da.SelectCommand.CommandType = CommandType.StoredProcedure;
        da.SelectCommand.Parameters.Add("@TableList", SqlDbType.VarChar, 200).Value = "*";
        da.SelectCommand.Parameters.Add("@TableName", SqlDbType.VarChar, 30).Value = "news";
        if(classid==0)
            da.SelectCommand.Parameters.Add("@SelectWhere", SqlDbType.VarChar, 500).Value = " classid in(311,312,313)";
        else
            da.SelectCommand.Parameters.Add("@SelectWhere", SqlDbType.VarChar, 500).Value = " classid =" + classid;
        da.SelectCommand.Parameters.Add("@SelectOrderId", SqlDbType.VarChar, 20).Value = "id";
        da.SelectCommand.Parameters.Add("@SelectOrder", SqlDbType.VarChar, 200).Value = "order by id desc";
        da.SelectCommand.Parameters.Add("@intPageNo", SqlDbType.Int).Value = pageNo;
        da.SelectCommand.Parameters.Add("@intPageSize", SqlDbType.Int).Value = pageSize;
        da.SelectCommand.Parameters.Add("@RecordCount", SqlDbType.Int).Direction = ParameterDirection.Output;
        da.SelectCommand.Parameters.Add("RowCount", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;



        DataSet ds = new DataSet();
        da.Fill(ds, "news");
        this.DataList1.DataSource = ds;
        this.DataList1.DataBind();

        Int32 RecordCount = (Int32)da.SelectCommand.Parameters["@RecordCount"].Value;
        Int32 RowCount = (Int32)da.SelectCommand.Parameters["RowCount"].Value;
        lbRecord.Text = RecordCount.ToString();
        lbRow.Text = pageNo.ToString();
        sumPage = (Int32)RecordCount / pageSize;

        if (RecordCount % pageSize > 0)
        {
            sumPage = sumPage + 1;
        }
        lbpage.Text = sumPage.ToString();
        if (pageNo > 1)
        {
            hylfirst.NavigateUrl = "?cid=" + classid + "&page=1";
            hylprev.NavigateUrl = string.Concat("?cid=" + classid + "&page=", "", pageNo - 1);
        }
        else
        {
            hylprev.NavigateUrl = "";
            hylfirst.NavigateUrl = "";
            hylfirst.Enabled = false;
            hylprev.Enabled = false;
        }
        if (pageNo < sumPage)
        {
            hylend.NavigateUrl = string.Concat("?cid=" + classid + "&page=", "", sumPage);
            hylnext.NavigateUrl = string.Concat("?cid=" + classid + "&page=", "", pageNo + 1);
        }
        else
        {
            hylnext.NavigateUrl = "";
            hylend.NavigateUrl = "";
            hylend.Enabled = false;
            hylnext.Enabled = false;
        }
    }
    protected void link_Click(object sender, EventArgs e)
    {
        int page = Convert.ToInt32(txtlink.Text);
        Response.Redirect("dzsw_anli.aspx?cid=" + Request.QueryString["page"].ToString() + "&page=" + page + "");
    }
}
